package com.sky.controller.admin;

import com.sky.dto.OrdersCancelDTO;
import com.sky.dto.OrdersConfirmDTO;
import com.sky.dto.OrdersPageQueryDTO;
import com.sky.dto.OrdersRejectionDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.OrderService;
import com.sky.vo.OrderStatisticsVO;
import com.sky.vo.OrderVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.Info;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController("adminOrderController")
@RequestMapping("/admin/order")
@Api(tags = "管理端订单相关接口")
@Slf4j
public class OrderController {

    @Autowired
    private OrderService orderService;
    /*
    * 订单搜索
    * */
    @GetMapping("/conditionSearch")
    @ApiOperation(value = "订单搜索")
    public Result<PageResult> findAllOrders(OrdersPageQueryDTO ordersPageQueryDTO) {
        log.info("管理端订单搜索：{}", ordersPageQueryDTO);
        PageResult pageResult = orderService.findHistoryOrders(ordersPageQueryDTO);
        return Result.success(pageResult);
    }

    /*
    * 各个状态的订单数量统计
    * */
    @GetMapping("/statistics")
    @ApiOperation(value = "各个状态的订单数量统计")
    public Result<OrderStatisticsVO> statisticsOrdersByStatus(){
        log.info("统计各个状态的订单总数");
        OrderStatisticsVO orderStatisticsVO = orderService.statisticsOrdersByStatus();
        return Result.success(orderStatisticsVO);
    }

    /*
    * 查看订单详细详细
    * */
    @GetMapping("/details/{id}")
    @ApiOperation(value = "查看订单详细详细")
    public Result<OrderVO> findOrderDetailsByOrderId(@PathVariable Long id) {
        log.info("管理端查看订单详细信息，订单id为：{}", id);
        OrderVO orderVO = orderService.findOrderById(id);
        return Result.success(orderVO);
    }

    /*
    * 管理端接单
    * */
    @PutMapping("/confirm")
    @ApiOperation(value = "管理端接单")
    public Result takeOrder(@RequestBody OrdersConfirmDTO ordersConfirmDTO){
        log.info("管理端接单，订单信息为：{}", ordersConfirmDTO);
        orderService.takeOrder(ordersConfirmDTO);
        return Result.success();
    }

    /*
    * 管理端拒单
    * */
    @PutMapping("/rejection")
    @ApiOperation(value = "管理端拒单")
    public Result rejectOrder(@RequestBody OrdersRejectionDTO ordersRejectionDTO){
        log.info("商家拒单：{}", ordersRejectionDTO);
        orderService.rejectOrder(ordersRejectionDTO);
        return Result.success();
    }

    /*
    * 管理端取消订单
    * */
    @PutMapping("/cancel")
    @ApiOperation(value = "管理端取消订单")
    public Result cancelOrder(@RequestBody OrdersCancelDTO ordersCancelDTO){
        log.info("商家取消订单：{}", ordersCancelDTO);
        orderService.cancelOrder(ordersCancelDTO);
        return Result.success();
    }

    /*
    * 派送订单
    * */
    @PutMapping("/delivery/{id}")
    @ApiOperation(value = "管理端派送订单")
    public Result deliveryOrder(@PathVariable Long id){
        log.info("派送订单，订单id为：{}", id);
        orderService.deliveryOrder(id);
        return Result.success();
    }

    /*
    * 完成订单
    * */
    @PutMapping("/complete/{id}")
    @ApiOperation(value = "管理端完成订单")
    public Result completeOrder(@PathVariable Long id){
        log.info("完成订单，订单id为：{}", id);
        orderService.completeOrder(id);
        return Result.success();
    }
}
